﻿namespace GuiaTest
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Guia.Core;
    using Guia.Elements;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using WpfTestApplication;

    /// <summary>
    /// Summary description for ComboBoxTest
    /// </summary>
    [TestClass]
    public class ComboBoxTest : AbstractWindowTest
    {
        protected override Type TestWindowType()
        {
            return typeof(ComboBoxTestWindow);
        }

        protected override string TestWindowName()
        {
            return "ComboBoxTestWindow";
        }

        [TestMethod]
        public void TestExpandCollapse()
        {
            ComboBox comboBox = window.Get<ComboBox>("comboBox1");
            Assert.IsFalse(comboBox.IsExpanded);
            comboBox.Expand();
            Assert.IsTrue(comboBox.IsExpanded);
            comboBox.Collapse();
            Assert.IsFalse(comboBox.IsExpanded);
            comboBox.IsExpanded = true;
            Assert.IsTrue(comboBox.IsExpanded);
            comboBox.IsExpanded = true;
            Assert.IsTrue(comboBox.IsExpanded);
            comboBox.IsExpanded = false;
            Assert.IsFalse(comboBox.IsExpanded);
            comboBox.IsExpanded = false;
            Assert.IsFalse(comboBox.IsExpanded);
        }

        [TestMethod]
        public void TestGetSelectedItem()
        {
            ComboBox comboBox = window.Get<ComboBox>("comboBox1");
            ComboBoxItem selItem = comboBox.SelectedItem;
            Assert.IsNull(selItem);
            comboBox.GetItem("item 1").Select();
            selItem = comboBox.SelectedItem;
            Assert.IsNotNull(selItem);
            Assert.AreEqual("item 1", selItem.TextValue);

            comboBox.GetItem("item 2").Select();
            selItem = comboBox.SelectedItem;
            Assert.IsNotNull(selItem);
            Assert.AreEqual("item 2", selItem.TextValue);
        }

        [TestMethod]
        public void TestGetItems()
        {
            ComboBox comboBox = window.Get<ComboBox>("comboBox1");
            ComboBoxItem[] items = comboBox.Items;
            Assert.AreEqual(3,items.Length);
            Assert.AreEqual("item 1",items[0].TextValue);
            Assert.AreEqual("item 2", items[1].TextValue);
            Assert.AreEqual("item 3", items[2].TextValue);
        }

        [TestMethod]
        public void TestGetItemsIntIndexer()
        {
            ComboBox comboBox = window.Get<ComboBox>("comboBox1");
            Assert.AreEqual("item 1", comboBox[0].TextValue);
            Assert.AreEqual("item 2", comboBox[1].TextValue);
            Assert.AreEqual("item 3", comboBox[2].TextValue);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void TestGetItemsIntIndexerInvalidValue()
        {
            ComboBox comboBox = window.Get<ComboBox>("comboBox1");
            ComboBoxItem item = comboBox[-1];
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void TestGetItemsIndexerNotFound()
        {
            ComboBox comboBox = window.Get<ComboBox>("comboBox1");
            Assert.AreEqual("item 1", comboBox[3].TextValue);
        }

        [TestMethod]
        public void TestContains()
        {
            ComboBox comboBox = window.Get<ComboBox>("comboBox1");
            Assert.IsTrue(comboBox.Contains("item 1"));
            Assert.IsTrue(comboBox.Contains("item 2"));
            Assert.IsTrue(comboBox.Contains("item 3"));
            Assert.IsFalse(comboBox.Contains("item 4"));
        }

        [TestMethod]
        public void TestGetItem()
        {
            ComboBox comboBox = window.Get<ComboBox>("comboBox1");
            ComboBoxItem item = comboBox.GetItem("item 3");
            Assert.IsNotNull(item);
            Assert.AreEqual("item 3",item.TextValue);
        }

        [TestMethod,ExpectedException(typeof(ElementNotFoundException))]
        public void TestGetItemNotExisting()
        {
            ComboBox comboBox = window.Get<ComboBox>("comboBox1");
            ComboBoxItem item = comboBox.GetItem("item 4");
        }

        [TestMethod]
        public void TestGetItemWithStringIndexer()
        {
            ComboBox comboBox = window.Get<ComboBox>("comboBox1");
            ComboBoxItem item = comboBox["item 2"];
            Assert.IsNotNull(item);
            Assert.AreEqual("item 2", item.TextValue);
        }

        [TestMethod, ExpectedException(typeof(ElementNotFoundException))]
        public void TestGetItemNotExistingWithStringIndexer()
        {
            ComboBox comboBox = window.Get<ComboBox>("comboBox1");
            ComboBoxItem item = comboBox["item 4"];
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetItemWithStringIndexerWithNullValue()
        {
            ComboBox comboBox = window.Get<ComboBox>("comboBox1");
            ComboBoxItem item = comboBox[null];
        }
    }
}
